package com.itic.system.order.ordervip.service;

import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.syssetting.persistence.dao.SysSettingDao;
import com.itic.appbase.applications.sys.user.persistence.dao.SysUserDao;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.CalendarHelper;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.RandomOrderNumHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.configuration.PropertyHelper;
import com.itic.appbase.framework.utils.wxpay.vo.Wxpay;
import com.itic.appbase.framework.xcx.utils.WeixinUtil;
import com.itic.appbase.framework.xcx.vo.WXPayResult;
import com.itic.system.market.viptype.persistence.dao.VipTypeDao;
import com.itic.system.market.viptype.persistence.model.VipType;
import com.itic.system.order.ordervip.persistence.dao.OrderVipDao;
import com.itic.system.order.ordervip.persistence.model.OrderVip;
import com.itic.system.order.ordervip.persistence.model.OrderVipExample;


@Service
@Transactional
public class OrderVipService
     extends CrudService<OrderVipDao,OrderVip,OrderVipExample> {
    @Autowired
    private OrderVipDao orderVipDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private VipTypeDao vipTypeDao;
    @Autowired
    private SysSettingDao sysSettingDao;
    
	public void accountPay(SysUser user, VipType vipType) {

		OrderVip orderVip = new OrderVip();
		orderVip.setUserId(user.getId());
		orderVip.setUserName(user.getUsername());
		orderVip.setVipType(vipType.getId());
		
	}

	/**
	 * 用户微信调用
	 * @param user
	 * @param vipType
	 * @return
	 */
	public WXPayResult addOrderAndReturnWxPay(SysUser user, VipType vipType) {
		OrderVip orderVip = new OrderVip();
		this.initDefalutFields(orderVip);
		orderVip.setStatus("noPay");
		orderVip.setTotalMoney(vipType.getPayMoney());
		orderVip.setUserId(user.getId());
		orderVip.setUserName(user.getUsername());
		orderVip.setVipType(vipType.getId());
		orderVip.setVipTypeName(vipType.getShowDesc());
		orderVip.setOrderNum(RandomOrderNumHelper.getOrderNumber());
		orderVipDao.insert(orderVip);
		
		String isOpenPay = sysSettingDao.getByKey("IS_OPEN_PAY");
		if( StringHelper.isBlank(isOpenPay) ){
			WXPayResult wxPayResult  = WeixinUtil.wxPay(user.getOpenId(), PropertyHelper.getString("wxpay.xcx.appid"), PropertyHelper.getString("wxpay.xcx.mchid"), PropertyHelper.getString("wxpay.xcx.key"), orderVip.getId(),0.01, "118.190.144.162", "惠发科技，订单号码："+orderVip.getOrderNum(),PropertyHelper.getString("wxpay.xcx.vip.notifyUrl"));
			return wxPayResult;
		}else{
			WXPayResult wxPayResult  = WeixinUtil.wxPay(user.getOpenId(), PropertyHelper.getString("wxpay.xcx.appid"), PropertyHelper.getString("wxpay.xcx.mchid"), PropertyHelper.getString("wxpay.xcx.key"), orderVip.getId(),vipType.getPayMoney(), "118.190.144.162", "惠发科技，订单号码："+orderVip.getOrderNum(),PropertyHelper.getString("wxpay.xcx.vip.notifyUrl"));
			return wxPayResult;
		}
		
	}

	/**
	 * 微信支付成功方法
	 * @param appid
	 * @param out_trade_no
	 * @param finishTime
	 * @param total_fee
	 * @param transaction_id
	 * @param openid
	 * @param mch_id
	 */
	public String paySuccessDealOrder(String appid, String out_trade_no, String finishTime, String total_fee,
			String transaction_id, String openid, String mch_id) {
		OrderVip orderVip = orderVipDao.selectByPrimaryKey(out_trade_no);
		if( null != orderVip ){
			orderVip.setStatus("success");
			try {
				orderVip.setPayTime(DateHelper.parseDate(finishTime, "yyyyMMddHHmmss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			orderVip.setPayNum(transaction_id);
			orderVipDao.updateByPrimaryKey(orderVip);
			//用户会员时间增加3个月
			String userId = orderVip.getUserId();
			SysUser sysUser = sysUserDao.selectByPrimaryKey(userId);
			sysUser.setIsVip("vip");
			
			VipType vipType = vipTypeDao.selectByPrimaryKey(orderVip.getVipType());
			Integer vipMonth = vipType.getVipMonth();
			Date vipExpiredTime = null;
			if( null != sysUser.getVipExpiredTime() ){
				vipExpiredTime = sysUser.getVipExpiredTime();
			}else{
				vipExpiredTime = new Date();
			}
			Date monthAfter = CalendarHelper.getMonthAfter(vipExpiredTime, vipMonth);
			sysUser.setVipExpiredTime(monthAfter);
			
			sysUserDao.updateByPrimaryKey(sysUser);
		}
		return SUCCESS;
	}


}